<template>
  <div>
    <!-- 查询表单 -->
    <el-form :inline="true" :model="query" style="margin-bottom: 16px;">
      <el-form-item label="发送者用户名：">
        <el-input v-model="query.sender" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 消息表格 -->
    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="id" label="Id" width="60" />
      <el-table-column prop="title" label="标题" width="120" />
      <el-table-column prop="content" label="内容" width="180" />
      <el-table-column prop="sender" label="发送人" width="120" />
      <el-table-column prop="receiver" label="接收人" width="180" />
      <el-table-column prop="readCount" label="已读数" width="80" />
      <el-table-column prop="receiverCount" label="接收人数" width="100" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
    </el-table>

    <!-- 分页 -->
    <div style="margin-top: 20px;">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size="query.pageSize"
        :current-page="query.pageIndex"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { fetchMessageList, MessageItem, MessageQuery } from '@/api/message'

const query = ref<MessageQuery>({
  sender: '',
  pageIndex: 1,
  pageSize: 10
})

const list = ref<MessageItem[]>([])
const total = ref(0)

const getList = async () => {
  const res = await fetchMessageList(query.value)
  list.value = res.list
  total.value = res.total
}

const sizeChange = (size: number) => {
  query.value.pageSize = size
  query.value.pageIndex = 1
  getList()
}
const pageChange = (page: number) => {
  query.value.pageIndex = page
  getList()
}

onMounted(getList)
</script>